<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Module_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function searchModuleDB($modulecode, $modulename){
          $sql;
          if($modulecode != "" && $modulename != ""){
            $sql = "SELECT * FROM app_modules WHERE module_code LIKE ".$this->db->escape($modulecode)." AND module_name LIKE '".$modulename."' ";
          }else if($modulecode == "" && $modulename != ""){
            $sql = "SELECT * FROM app_modules WHERE module_name LIKE ".$this->db->escape($modulename)." ";
          }else{
            $sql = "SELECT * FROM app_modules WHERE module_code LIKE ".$this->db->escape($modulecode)." ";
          }
          //$query = $this->db->get_where('app_modules', array('module_code' => $modulecode, 'module_name' => $modulename), 1);
          $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }
    }
    public function save_module($data){
    $this->db->query("call app_modules_ins(?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
    $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
    $id; $msg;$err;
    foreach($que->result() as $keys){
      $id = $keys->id;
      $err = $keys->err;
      $msg = $keys->msg;
    }
    $arr = array(
        'modID'  =>$id,
        'err'    =>$err,
        'msg'    =>$msg
    );
      echo json_encode($arr);
    }
    public function insertModuleMenu($data){
      $query = $this->db->query("call app_module_menu_ins(?,?,?,@gID,@err,@msg)",$data);
      $que = $this->db->query("SELECT @gID as id,@err,@msg as msg");
      foreach($que->result() as $keys){
      $id = $keys->id;
      $msg = $keys->msg;
      }
      $this->db->where('menu_id',$id);
      $query = $this->db->get('app_module_menu');
      foreach($query->result() as $keys){
          $name = $keys->menu_name;
      }
      $arr = array(
          'modID'=>$id,
          'msg'=>$name
      );
      return $arr;
    }
    public function getModbyID($id){
      $query = $this->db->select('*')
                        ->from('app_modules')
                        ->where('module_id',$id)
                        ->get();
      return $query->result(); 
    }
    public function getModuleMenu($id){

        $this->db->where('module_id', $id);
        $query = $this->db->get('app_module_menu');
        return $query->result();
    }
    public function getModuleFormMenu($id){
      $sql = "SELECT amm.menu_name menu_name,amm.menu_id menu_id
              FROM app_modules am
                INNER JOIN app_module_menu amm ON am.module_id = amm.module_id 
              WHERE am.module_id = ".$id."
        ";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function edit_menus($data){
      $this->db->query("call app_module_menu_upd(?,?,?,@err_code,@err_msg)",$data);
      $err ; $msg;
      $que = $this->db->query("SELECT @err_code as error, @err_msg as err_msg");
      return $que->result();
    }
    public function edit_module($data){
    $this->db->query("call app_modules_upd(?,?,?,?,?,@err,@msg)",$data);
    $que = $this->db->query("SELECT @err as err,@msg as msg");
    $id; $msg;
    foreach($que->result() as $keys){
      $id = $keys->err;
      $msg = $keys->msg;
    }
    $arr = array(
        'modID'=>$id,
        'msg'=>$msg
    );
      echo json_encode($arr);
    }

}